package com.dhcc.producter.advance.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.ExchangeBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqDeadLetterConfig {
	// 死信交换机
	public static final String DLX_EXCHANGE_NAME = "my-dlx-exchange";

	// 正常交换机
	public static final String PROCESS_EXCHANGE_NAME = "process-exchange";

	// 死信队列
	public static final String DEAD_LETTER_QUEUE_NAME = "dead-letter-queue";

	// 业务处理队列
	public static final String PROCESS_QUEUE_NAME = "process-queue";
	
	// 死信队列的routingKEY
	public static final String DLX_ROUTING_KEY = "dead";

	// 业务处理队列的routingKEY
	public static final String PROCESS_ROUTING_KEY = "info";
	
	// 创建dlx
	@Bean
	public Exchange dlxExchange() {
		return ExchangeBuilder.directExchange(DLX_EXCHANGE_NAME).durable(true).build();
	}

	// 创建死信队列
	@Bean
	public Queue deadLetterQueue() {
		return QueueBuilder.durable(DEAD_LETTER_QUEUE_NAME).build();
	}

	// 绑定死信队列
	@Bean
	public Binding dlxBinding() {
		return BindingBuilder.bind(deadLetterQueue()).to(dlxExchange()).with(DLX_ROUTING_KEY).noargs();
	}

	// 创建正常交换机
	@Bean
	public Exchange processExchange() {
		return ExchangeBuilder.directExchange(PROCESS_EXCHANGE_NAME).durable(true).build();
	}

	// 创建正常队列
	@Bean
	public Queue processQueue() {
		return QueueBuilder.durable(PROCESS_QUEUE_NAME)
					.maxLength(10) // 设置队列最大容量
					.deadLetterExchange(DLX_EXCHANGE_NAME) // 绑定死信交换机
					.deadLetterRoutingKey(DLX_ROUTING_KEY)
					.build();
	}
	
	// 绑定正常队列
	@Bean
	public Binding processBinding() {
		return BindingBuilder.bind(processQueue()).to(processExchange()).with(PROCESS_ROUTING_KEY).noargs();
	}

}
